<?php
namespace app\admin\controller;
use clt\Leftnav;
use app\admin\model\Party;

/**
 * 扶贫新闻管理
 */
class News extends Common
{
	public function index()
	{
		if(request()->isPost())
        {
            $key  = input('post.key', '', 'trim');
            $page = input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');

            $data = db('news_cate') -> select();
            $ids  = $this -> cateSons($data, input('get.cid'));
            $ids[]= input('get.cid/d');

            $list = db('poor_news n')
                 -> join('news_cate c','c.id=n.cate_id','left')
                 -> field('n.id,n.title news_title,pub_time,source,pub_person,c.title cate_title,case n.light when 1 then "是"  else "否" end light,case n.commend when 1 then "是"  else "否" end commend')
                 -> where('c.title|n.title','like',"%".$key."%")
                 -> where(array('n.cate_id'=>array('in',$ids)))
                 -> paginate(array('list_rows'=>$pageSize,'page'=>$page))
                 -> toArray();
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        // $this->assign('cid',input('get.cid'));
		return view();
	}


	/**
     * 添加
     */
	public function add()
	{
		if (request()->isPost()) {
            $data = input('post.');
            //添加
            if (db('poor_news')->insert($data)) {
                return ['code'=>1,'msg'=>'添加成功!','url'=>url('index')."?cid=".input('param.cid')];
            } else {
                return ['code'=>0,'msg'=>'添加失败!'];
            }
        }

		//输出分类
        $data = db('news_cate') -> select();
        $ids  = $this -> cateSons($data, input('get.cid'));
        $ids[]= input('get.cid/d');
        $list = db('news_cate') -> where(['id'=>['in',$ids]]) -> select();
        $this-> assign('list',$list);
		return view();
	}

    



	/**
     * 修改
     */
	public function edit()
	{
		if (request()->isPost()) {
            $data = input('post.');
            if (db('poor_news')->update($data) !== false) {
                return ['code'=>1,'msg'=>'修改成功!','url'=>url('index')."?cid=".input('param.cid')];
            } else {
                return ['code'=>0,'msg'=>'修改失败!'];
            }
        }


        $id   = input('param.id');
        $news = db('poor_news') -> find($id);
        $this -> assign('info', $news);
        // halt($news);

		//输出分类
    	$data = db('news_cate') -> select();
        $ids  = $this -> cateSons($data, input('param.cid'));
        $ids[]= input('param.cid');
        $list = db('news_cate') -> where(['id'=>['in',$ids]]) -> select();
        $this-> assign('list',$list);
		return view();
	}

	/**
     * 删除
     */
    public function del()
    {
        $id   = input('param.id');
        $info = db('poor_news') -> delete($id);
        if ($info) {
            return ['code'=>1,'msg'=>'删除成功！'];
        } else {
            return ['code'=>1,'msg'=>'删除失败！'];
        }        
    
    }

	/**
     * 批量删除
     */
    public function delAll()
    {
        $map['id'] = array('in',input('param.ids/a'));
        $info = db('poor_news') -> where($map)->delete();
        if ($info) {
           return ['code'=>1,'msg'=>'删除成功！','url'=>url('index')];
        } else {
        	return ['code'=>1,'msg'=>'删除成功！','url'=>url('index')];
        }    
    }

    /**
     * 分类
     */
    public function cateSons($data, $pid)
    {
        if ($data)
        {
            static $arr = [];
            foreach($data as $key => $vo)
            {
                if ($vo['pid'] == $pid)
                {
                    $arr[] = $vo['id'];
                    $this -> cateSons($data, $vo['id']);
                }
            }
            return $arr;
        }
    }
}